//
//  UIColor+RgbAndString.m
//  BlueToothProject
//
//  Created by foshou on 2017/3/2.
//  Copyright © 2017年 yxw. All rights reserved.
//

#import "UIColor+RgbAndString.h"

@implementation UIColor (RgbAndString)

// 将16进制字符串转换成uicolor
+(UIColor*)toUIColorByStr:(NSString*)colorStr{
    
    NSString *cString = [[colorStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

// 注意转换出来的字符串不带＃号
+(NSString*)toStrByUIColor:(UIColor*)color{
//    CGFloat r, g, b, a;
//    [color getRed:&r green:&g blue:&b alpha:&a];
//    int rgb = (int) (r * 255.0f)<<16 | (int) (g * 255.0f)<<8 | (int) (b * 255.0f)<<0;
//    return [NSString stringWithFormat:@"%06x", rgb];
    return [self changeUIColorToRGB:color];
}

//颜色转字符串
+(NSString *) changeUIColorToRGB:(UIColor *)color{
    
    
    const CGFloat *cs=CGColorGetComponents(color.CGColor);
    
    NSString *r = [NSString stringWithFormat:@"%@",[self ToHex:cs[0]*255.0f]];
    NSString *g = [NSString stringWithFormat:@"%@",[self ToHex:cs[1]*255.0f]];
    NSString *b = [NSString stringWithFormat:@"%@",[self ToHex:cs[2]*255.0f]];
    NSString *a = [NSString stringWithFormat:@"%@",[self ToHex:cs[3]*255.0f]];
    return [NSString stringWithFormat:@"%@%@%@%@",r,g,b,a];
}


//十进制转十六进制
+(NSString *)ToHex:(int)tmpid
{
    NSString *endtmp=@"";
    NSString *nLetterValue;
    NSString *nStrat;
    int ttmpig=tmpid%16;
    int tmp=tmpid/16;
    switch (ttmpig)
    {
        case 10:
            nLetterValue =@"A";break;
        case 11:
            nLetterValue =@"B";break;
        case 12:
            nLetterValue =@"C";break;
        case 13:
            nLetterValue =@"D";break;
        case 14:
            nLetterValue =@"E";break;
        case 15:
            nLetterValue =@"F";break;
        default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig];
            
    }
    switch (tmp)
    {
        case 10:
            nStrat =@"A";break;
        case 11:
            nStrat =@"B";break;
        case 12:
            nStrat =@"C";break;
        case 13:
            nStrat =@"D";break;
        case 14:
            nStrat =@"E";break;
        case 15:
            nStrat =@"F";break;
        default:nStrat=[[NSString alloc]initWithFormat:@"%i",tmp];
            
    }
    endtmp=[[NSString alloc]initWithFormat:@"%@%@",nStrat,nLetterValue];
    return endtmp;
}



@end
